package cn.java.lambda.test3;

import java.util.Random;
import java.util.function.Supplier;

/**
 * 测试Supplier接口
 * @author 攀博课堂(www.pbteach.com)
 * @version 1.0
 **/
public class TestSupplier {

    static long sequence = 0;

    //生成不同类型的编号，比如：订单号、课程编号等，采用方法是：随机，顺序编号，时间编号

    //定义随机数的生成器,Math.abs获取绝对值
    static Supplier<Long> randomNum = () -> Math.abs(new Random().nextLong());

    //顺序编号
    static Supplier<Long> sequenceNum = () -> ++sequence;

    //时间编号,获取当前时间纳秒值
    static Supplier<Long> timeNum = () -> System.nanoTime();

    public static void main(String[] args) {

        System.out.println("=========生产随机数========");
        for (int i = 0; i < 10; i++) {
            System.out.println(randomNum.get());
        }
        System.out.println("=========生产顺序编号========");
        for (int i = 0; i < 10; i++) {
            System.out.println(sequenceNum.get());
        }
        System.out.println("=========生产时间编号========");
        for (int i = 0; i < 10; i++) {
            System.out.println(timeNum.get());
        }
    }

}
